const { app, BrowserWindow } = require('electron')
const WinState = require('electron-win-state').default
const path = require('path')
require('./controller/getSource')
require('./controller/alert')
require('./controller/openWindow')
require('./controller/getFileList')
require('./controller/buildMenu')
const createTray = require('./tray')

const createWindow = () => {
  // 记录窗口状态
  const winState = new WinState({
    defaultWidth: 1200,
    defaultHeight: 800,
    electronStoreOptions: {
      name: 'window-state-main'
    }
  })
  const win = new BrowserWindow({
    ...winState.winOptions,
    show: false,
    webPreferences: {
      preload: path.join(__dirname, './preload/preload.js')
    }
  })

  win.once('ready-to-show', () => {
    win.show()
  })

  win.loadURL('http://localhost:5173/')

  // win.webContents.openDevTools()

  // 记录窗口状态
  winState.manage(win)

  createTray(app, win)
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
